home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / netlib / _dup2.c < prev    next >
C/C++ Source or Header  |  1994-04-12  |  1KB  |  75 lines

  1. RCS_ID_C="$Id: _dup2.c,v 3.2 1994/04/12 20:43:21 jraja Exp $";
  2. /*
  3.  * _dup2.c - duplicate a file descriptor for SAS C 
  4.  *
  5.  * Author: ppessi <Pekka.Pessi@hut.fi>
  6.  *
  7.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
  8.  *                  Helsinki University of Technology, Finland.
  9.  *                  All rights reserved.
  10.  *
  11.  * Created      : Thu Mar 24 05:19:36 1994 ppessi
  12.  * Last modified: Thu Mar 24 07:58:07 1994 ppessi
  13.  *
  14.  */
  15.  
  16. #include <ios1.h>
  17. #include <fcntl.h>
  18. #include <stdlib.h>
  19. #include <dos.h>
  20. #define USE_BUILTIN_MATH
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <dos/dos.h>
  24. #include <proto/dos.h>
  25.  
  26. #include <bsdsocket.h>
  27.  
  28. /****** net.lib/dup2 **********************************************************
  29.     SEE ALSO
  30.         dup()
  31. *******************************************************************************
  32. */
  33.  
  34.  
  35. int
  36. __dup2(int old_fd, int new_fd)
  37. {
  38.   struct UFB *ufb;
  39.   int ufbflg;
  40.  
  41.   /*
  42.    * Check if there is nothing to do
  43.    */
  44.   if (old_fd == new_fd)
  45.     return old_fd;
  46.  
  47.   /*
  48.    * Check for the break signals
  49.    */
  50.   __chkabort();
  51.  
  52.   __close(new_fd);
  53.  
  54.   /*
  55.    * Find the ufb * for the old FD
  56.    */
  57.   if ((ufb = __chkufb(old_fd)) == NULL) {
  58.     errno = EBADF;
  59.     return -1;
  60.   }
  61.  
  62.   ufbflg = ufb->ufbflg;
  63.  
  64.   /* 
  65.    * The brain dead UFB system won't allow duplicating ordinary files
  66.    */
  67.   if ((ufbflg & UFB_SOCK) == UFB_SOCK) {
  68.     return Dup2Socket(old_fd, new_fd);
  69.   } else {
  70.     errno = EBADF;
  71.     return -1;
  72.   }
  73.  
  74. }
  75.